package basic.a6;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class Countrydao2 {
	
	public List<Countries> findAll(){
		List<Countries> cts=new ArrayList<>();
		Connection con=Util.getconn();
		
		try {
			Statement stm=con.createStatement();
			ResultSet rs= stm.executeQuery("select*from countries");
			while(rs.next()){
				Countries c=new Countries();
				c.setCountry_id(rs.getString(1));
				c.setCountry_name(rs.getString(2));
				c.setRegion_id(rs.getInt(3));
				cts.add(c);
			}
			Util.close(rs);
			Util.close(stm);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return cts;
	}
	
	public void insert(Countries c){
		Connection con=Util.getconn();
		try {
			PreparedStatement pstm=con.prepareStatement("insert into countries values (?,?,?)");
			pstm.setString(1, c.getCountry_id());
			pstm.setString(2, c.getCountry_name());
			pstm.setInt(3, c.getRegion_id());
			pstm.executeUpdate();
			Util.close(pstm);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void delete(String s){
		Connection con=Util.getconn();
		try {
			PreparedStatement pstm=con.prepareStatement(s);
			pstm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
